// 获取用户信息
import {showMessage} from '../../js/my-plugin'
import {getHttpPath} from '../../js/my-utils'

export const navs = [
  {id: 0, title: '大会员', href: '/#/account/big'},
  {id: 1, title: '消息', href: '/'},
  {id: 2, title: '动态', href: '/'},
  {id: 3, title: '追番', href: '/#/anime/collect'},
  {id: 4, title: '收藏', href: '/'},
  {id: 5, title: '历史', href: '/'}
]

export const navBars = [
  {id: 0, title: '首页', href: '/'},
  {id: 1, title: '动漫', href: '/#/anime'},
  {id: 2, title: '漫画', href: '/#/manga'},
  {id: 3, title: '小说', href: '/#/novel'},
  {id: 4, title: '新番时间表', href: '/#/anime/timeline'},
  {id: 5, title: '排行榜', href: '/#/rank'}
]

export const getInfoApi = function (_this, authorization) {
  const tagArr = ['正式会员', '大会员', '年度大会员']
  _this.$axios({
    method: 'get',
    url: '/api/user/get_info',
    headers: {Authorization: authorization}
  })
    .then((res) => {
      if (res.data.code === 200) {
        _this.isLogged = true
        _this.user.nickName = res.data.data.nickname
        _this.user.userIcon = getHttpPath(res.data.data.avatar)
        _this.user.exp = res.data.data.exp
        _this.user.level = res.data.data.lev
        _this.user.totalExp = res.data.data.totalExp
        _this.user.isVip = false
        _this.user.isMedia = false
        _this.user.tag = tagArr[res.data.data.tag]
        const authorities = res.data.data.authorities
        for (let auth of authorities) {
          if (auth.authority === 'VIP') {
            _this.user.isVip = true
          }
          if (auth.authority === 'MEDIA') {
            _this.user.isMedia = true
          }
        }
        // 将用户信息存入state
        _this.$store.commit('setUser', res.data.data)
      } else if (res.data.status === 204) {
        // 更新token
        _this.authorization = res.data.data
        window.localStorage.setItem('authorization', res.data.data)
        _this.reload()
      }
    })
    .catch((error) => {
      console.log('error:', error)
    })
}

// 退出登录
export const logout = (_this) => {
  _this.$axios({
    method: 'get',
    url: '/api/user/logout',
    headers: {Authorization: _this.authorization}
  })
    .then((res) => {
      console.log(res.data)
      if (res.data.code === 200) {
        _this.isLogged = false
        _this.user.userIcon = require('../../assets/img/user/akari.jpg')
        showMessage(res.data.msg, 1)
        _this.authorization = ''
        _this.$store.commit('setToken', '')
        window.localStorage.clear()
      }
    })
    .catch((error) => {
      console.log(error)
    })
}
